var server = require('../../../utils/server');
var timeJSON = require('../../../utils/util').timeJSON;
var app = getApp();
Page({
  data: {
    filterId: 1,
    searchWords: '',
    placeholder: '烤鸭',
    shops: app.globalData.shops,
    sortsIndex:[0,0],
    sorts: []
  },
  onShow: function () {
  },
  onLoad: function () {
    this.getSorts();
  },
  getSorts: function () {  //获取菜系分类
    let that = this;
    server.postJSON('getSorts', function (res) {
      if (res.data.status === 200) {
        that.setData({
          sorts: [res.data.data, timeJSON()]
        })
        that.bindPickerList();
      }
    });
  },
  bindPickerList: function (e) {
    var self = this;
    var idx = this.data.sortsIndex;
    server.postJSON('searchFoodList', {
      _id: self.data.sorts[0][idx[0]]._id,
      starTime: self.data.sorts[1][idx[1]].starTime,
      endTime: self.data.sorts[1][idx[1]].endTime,
      pageIndex: 0
    }, function (res) {
      if (res.data.status === 200) {
        self.setData({
          showResult: true,
          shops: res.data.data
        });
      }
    });
  },
  bindColChange: function(e){
    var idx = this.data.sortsIndex;
    idx[e.detail.column] = e.detail.value;
    this.setData({
      sortsIndex: idx
    })
  },
  inputSearch: function (e) {
    this.setData({
      searchWords: e.detail.value
    });
  },
  editFood: function(e){
    wx.navigateTo({
      url: '../editor/index?_id=' + e.currentTarget.dataset.id +'&type=edit',
    })
  },
  delFood: function (e) {
    var self = this;
    var selectData = e.currentTarget.dataset;
    var shopData = self.data.shops;
    server.postJSON('delFood', {
      _id: selectData.id
    }, function (res) {
      if (res.data.status === 200) {
        shopData.splice(selectData.idx,1)
        self.setData({
          shops: shopData
        });
      }
    });
  },
  doSearch: function () {
    let self = this;
    server.postJSON('searchFood', {
      key: self.data.searchWords,
      pageIndex: 0
    }, function (res) {
      if (res.data.status === 200) {
        self.setData({
          showResult: true,
          shops: res.data.data
        });
      }
    });
  }
});

